package org.iotashan.oauth
{
	import org.iotashan.utils.URLEncoding;

	import com.hurlant.crypto.Crypto;
	import com.hurlant.util.der.PEM;
	import com.hurlant.crypto.hash.SHA1;
	import com.hurlant.crypto.rsa.RSAKey;
	import com.hurlant.util.Base64;
	import com.hurlant.util.Hex;

	import flash.utils.ByteArray;
	
	public class OAuthSignatureMethod_RSA_SHA1 implements IOAuthSignatureMethod
	{
		public function OAuthSignatureMethod_RSA_SHA1()
		{
		}
		
		public function get name():String {
			return "RSA-SHA1";
		}
		
		public function signRequest(request:OAuthRequest):String {
			// get the signable string
			var toBeSigned:String = request.getSignableString();
			var sha1:SHA1 = new SHA1();

			toBeSigned = 'GET&http%3A%2F%2Foffice.mgamesolutions.com%2F105%2Fadminservices.aspx&Balance%3D1000%26Command%3DFunBalanceAddX%26UID%3Ddevgif%26oauth_consumer_key%3DFPA%26oauth_nonce%3D4ddfca89acbef%26oauth_signature_method%3DRSA_SHA1%26oauth_timestamp%3D1306512009%26oauth_token%3D%26oauth_version%3D1.0';

			var rsa:RSAKey = PEM.readRSAPrivateKey(request.consumer.secret);
			var data:ByteArray = Hex.toArray(Hex.fromString(toBeSigned));
			var digest:ByteArray = sha1.hash(data);
			
			var result:ByteArray = new ByteArray();
			rsa.sign(digest, result, digest.length);
			
			var ret:String = Base64.encodeByteArray(result);
			
			trace('success? ', ('u0VjfVcIkBdOoTbvVEXga36N3u%2FoLSP4o5Oldxa4sGJIZvbxxvFwR9%2FeuJ5lXOo3g4L05IxUeMIPKWOzIoUewC3WcufDUn5pbBHh7%2BFBYyO1GYTuXSEoKENzVYTpuegubWSuXmD3eYPNq7i7ANOr8bCIHuDxDFHvVLAlKC6II48%3D' == ret));
			trace('signature:',ret);
			
			return ret;
		}
	}
}